package cn.fuxi2022.accounts.register.service.impl;

import cn.fuxi2022.accounts.activity.service.GroupActivityService;
import cn.fuxi2022.accounts.message.service.WeChatMessageService;
import cn.fuxi2022.accounts.register.mapper.GroupRegisterMapper;
import cn.fuxi2022.accounts.register.service.GroupRegisterService;
import cn.fuxi2022.accounts.register.service.WeChatQR;
import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.feign.api.GroupStudent;
import cn.fuxi2022.feign.api.GroupStudentInfoApi;
import cn.fuxi2022.model.empty.accounts.GroupActivity;
import cn.fuxi2022.model.empty.accounts.GroupRegister;
import cn.fuxi2022.model.empty.applets.GroupStudentInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 社团活动签到登记表 服务实现类
 * </p>
 *
 * @author Fuxi
 * @since 2022-10-19 16:55:19
 */
@Service
public class GroupRegisterServiceImpl extends ServiceImpl<GroupRegisterMapper, GroupRegister> implements GroupRegisterService {


    @Autowired
    private GroupActivityService groupActivityService;

    @Autowired
    private WeChatQR weChatQR;

    @Autowired
    private GroupStudent groupStudent;

    @Autowired
    private GroupStudentInfoApi groupStudentInfoApi;

    @Override
    public String startRegister(String activityId) {
        // 1.判断活动是否在进行中
        GroupActivity groupActivity = groupActivityService.getById(activityId);
        if (groupActivity.getStatus() != 1) {
            throw new SmartGroupException("活动未开始或结束，无法进行签到");
        }

        // 2.如果之前已经开始签到，生成过二维码，再获取微信二维码链接返回即可
        if (StringUtils.hasText(groupActivity.getRegisterImageUrl())) {
            return groupActivity.getRegisterImageUrl();
        }

        // 说明当前活动没有生成二维码
        // 3.计算二维码的有效时间（活动的结束时间+1分钟）
        long currentTimeMillis = System.currentTimeMillis();
        long time = groupActivity.getEndTime().getTime();

        long expireSeconds = time - currentTimeMillis + 60L * 1000L;

        // 4.获取签到二维码，将活动ID作为二维码构建参数，将有效时间传入
        String qrUrl = weChatQR.getQR(activityId, expireSeconds);

        // 5.保存签到二维码
        groupActivity.setRegisterImageUrl(qrUrl);
        groupActivityService.updateById(groupActivity);

        // 6.返回新生成的二维码
        return qrUrl;
    }

    @Autowired
    private WeChatMessageService weChatMessageService;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    @Override
    public void studentRegister(String activityId, String gzOpenId) {
        // 1.获取当前时间
        Date date = new Date();

        // 2.根据gzOpenId获取学生
        cn.fuxi2022.model.empty.auth.GroupStudent student = groupStudent.getStudentByOpenidGz(gzOpenId);

        if (ObjectUtils.isEmpty(student)) {
            weChatMessageService.pushRegister(gzOpenId,1,"未注册的用户",dateFormat.format(date));
            return;
        }

        // 3.根据学生ID获取学生详细信息
        GroupStudentInfo studentInfo = groupStudentInfoApi.getGroupStudentInfoByStudentId(student.getId());

        if (ObjectUtils.isEmpty(studentInfo)) {
            weChatMessageService.pushRegister(gzOpenId,1,"未完善信息的用户",dateFormat.format(date));
            return;
        }

        // 4.判断活动是否在进行中
        GroupActivity groupActivity = groupActivityService.getById(activityId);
        if (groupActivity.getStatus() != 1) {
            weChatMessageService.pushRegister(gzOpenId,1,studentInfo.getRealName(),dateFormat.format(date));
            return;
        }

        // 4.判断学生是否已经成功签到
        LambdaQueryWrapper<GroupRegister> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupRegister::getStudentId, student.getId()).eq(GroupRegister::getActivityId, activityId);
        if(this.count(wrapper) > 0){
            // 已经签到视作签到成功
            weChatMessageService.pushRegister(gzOpenId,0,studentInfo.getRealName(),dateFormat.format(date));
            return;
        }

        // 3.构建学生签到对象
        GroupRegister register = new GroupRegister();
        // 设置学生ID
        register.setStudentId(student.getId());
        // 设置学生姓名
        register.setStudentName(studentInfo.getRealName());
        // 设置活动ID
        register.setActivityId(activityId);

        // 开始签到
        this.save(register);

        weChatMessageService.pushRegister(gzOpenId,0,studentInfo.getRealName(),dateFormat.format(date));
    }
}
